package com.szewec.ps.myview;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.IntDef;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.szewec.ps.R;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
 * 右上角带图标的小标题
 */
public class TitleViewWithAdd extends ConstraintLayout implements BaseInput {
    protected ImageView leftImg;
    protected TextView title;
    protected FrameLayout iron;
    protected View dividingLine;

    protected ImageView editBtn;
    protected TextView addBtn;

    public final static int ADD = 0;

    public final static int EDIT = 1;

    /**
     * @hide
     */
    @IntDef({ADD, EDIT})
    @Retention(RetentionPolicy.SOURCE)
    public @interface RightType {
    }

    /**
     * 查看模式下是否隐藏图标,默认true
     */
    private Boolean hiddenIronWhenViewMode;
    private int rightType;

    public TitleViewWithAdd(Context context) {
        super(context);
    }

    public TitleViewWithAdd(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public TitleViewWithAdd(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        LayoutInflater.from(context).inflate(R.layout.title_view_with_add, this);
        leftImg = findViewById(R.id.iv_left_img);
        title = findViewById(R.id.tv_title);
        iron = findViewById(R.id.iv_iron);
        dividingLine = findViewById(R.id.dividing_line);
        editBtn = findViewById(R.id.iv_edit);
        addBtn = findViewById(R.id.tv_add);
        parseStyle(context, attrs);
    }

    private void parseStyle(Context context, AttributeSet attrs) {
        if (attrs != null) {
            TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.TitleViewWithAdd);
            String t = ta.getString(R.styleable.TitleViewWithAdd_title);
            title.setText(t);
            boolean bold = ta.getBoolean(R.styleable.TitleViewWithAdd_bold, false);
            title.getPaint().setFakeBoldText(bold);
            hiddenIronWhenViewMode = ta.getBoolean(R.styleable.TitleViewWithAdd_hiddenIronWhenViewMode, true);
            boolean isShowDividingLine = ta.getBoolean(R.styleable.TitleViewWithAdd_isShowDividingLine, false);
            if (isShowDividingLine) {
                hiddenIronWhenViewMode = false;
                title.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(R.dimen.sp_15));
                dividingLine.setVisibility(VISIBLE);
            }

            boolean required = ta.getBoolean(R.styleable.TitleViewWithAdd_addRequired, false);
            if (!required) {
                title.setCompoundDrawables(null, null, null, null);
            }
            int resourceId = ta.getResourceId(R.styleable.TitleViewWithAdd_leftImg, R.color.white);
            if (resourceId == R.color.white) {
                leftImg.setVisibility(GONE);
            } else {
                leftImg.setVisibility(VISIBLE);
                leftImg.setImageResource(resourceId);
            }
            rightType = ta.getInt(R.styleable.TitleViewWithAdd_rightType, 0);
            setRightType(rightType);
            ta.recycle();
        }
    }

    /**
     * 图标点击事件
     */
    private OnClickListener onClickListener;

    public void setIronClickListener(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
        if (rightType == 0) {
            iron.setOnClickListener(onClickListener);
            this.setOnClickListener(null);
        } else {
            iron.setOnClickListener(null);
            this.setOnClickListener(onClickListener);
        }
    }

    /**
     * 设置标题
     *
     * @param title
     */
    public void setTitle(String title) {
        this.title.setText(title);
    }

    @Override
    public void switchoverMode(boolean isEnabled) {
        if (hiddenIronWhenViewMode) {
            if (isEnabled) {
                iron.setVisibility(VISIBLE);
            } else {
                iron.setVisibility(GONE);
            }
        }
    }

    public void setRightType(@RightType int rightType) {
        this.rightType = rightType;
        if (rightType == EDIT) {
            editBtn.setVisibility(VISIBLE);
            addBtn.setVisibility(GONE);
        } else {
            editBtn.setVisibility(GONE);
            addBtn.setVisibility(VISIBLE);
        }
        setIronClickListener(this.onClickListener);
    }

    public String getType() {
        if (rightType == 1) {
            return "edit";
        }
        return "add";
    }
}
